/* Heterogeneity by any conservation/CRP participation */
clear
set more off 
eststo clear 

******************************** Pooled sample estimates ********************************
est clear 

use "$dta/SSpooled.dta", clear 

rename default treat
la def treat 0 "0%"  1 "100%" , replace 
la val treat treat 
la var treat "Default"


gen expSS1 = inag1and2 == 1 

gen expCRP = iscrp == 1

gen expSS1crp = inag1and2 == 1 | iscrp == 1

**** experience is CRP only ****

glm fracbid i.treat##i.expCRP  age agyears owner i.gender i.state  i.itemnum i.agvise , link(logit) family(binomial) vce(cluster person_id)
margins , dydx(treat) over(expCRP) post
estimates store ESTCRP
lincom _b[1.treat:1.expCRP] - _b[1.treat:0.expCRP]
estadd scalar deltab = r(estimate)
estadd scalar deltase = r(se)
estadd scalar ub = r(ub)
estadd scalar lb = r(lb)

***** experience is CRP or any other program ***

gen anycrp = iscrp == 1 | noncrp == 1
replace anycrp = 1 if inag1and2 == 1 & agvise == 2
gen treatanycrp = treat*anycrp

glm fracbid i.treat##i.anycrp  age agyears owner i.gender i.state  i.itemnum i.agvise , link(logit) family(binomial) vce(cluster person_id)

margins , dydx(treat) over(anycrp) post
estimates store ESTSS1ANYCRP
lincom _b[1.treat:1.anycrp] - _b[1.treat:0bn.anycrp] 
estadd scalar deltab = r(estimate)
estadd scalar deltase = r(se)
estadd scalar ub = r(ub)
estadd scalar lb = r(lb)

******************************** SS II sample estimates ********************************
    ** SS1 as experience using SS2 sample only 

use "$dta/SS1and2.dta", clear
gen expSS1 = inag1and2 == 1 

rename personid person_id
rename slidervariation treat 


glm realbid i.treat##i.expSS1 i.gender i.itemnum totalacres age productionyears rowcropacres i.state   if agvise == 2 , link(logit) family(binomial) vce(cluster person_id) nolog

margins , dydx(treat) over(expSS1) post
estimates store ESTSS1ag2
lincom _b[1.treat:1.expSS1] -  _b[1.treat:0.expSS1]
estadd scalar deltab = r(estimate)
estadd scalar ub = r(ub)
estadd scalar lb = r(lb)


**** output table ***

#delimit ;
esttab ESTSS1ANYCRP ESTCRP ESTSS1ag2 using
 "$outputs/02_TreatmentEffect_by_experience.tex", 
 b(2) ci(2) level(95) nostar 
 keep(1.treat:) eqlabels("", none)
 	rename(1.treat:0.anycrp a 1.treat:1.anycrp b 
		   1.treat:0.expCRP a 1.treat:1.expCRP b
		   1.treat:0.expSS1 a 1.treat:1.expSS1 b)
  coeflabels(a "\shortstack{Anchor 100\% $\times$ \\ No Experience}" 
	b  "\shortstack{Anchor 100 \% $\times$ \\ Experience}" )
 mtitles("\shortstack{Experience = CRP \\ or Other \\ Pooled Sample}" 
		"\shortstack{Experience = CRP \\ Pooled Sample}" 
		"\shortstack{Experience = SS I \\ SS II Sample}")
 stats(dumm1 dumm2 deltab lb ub N , fmt(%9.0gc %9.0gc  %03.2f %03.2f  %03.2f %9.0gc)
 labels("(Experience -" "\shortstack{ No Experience) \\ $\times$ Anchor}"
		"Estimate" "CI" "Observations") 
		layout("@" "@" "@" `""[@ @]""' "@")) 
 nonote  booktabs 
 replace ;
#delimit cr
